package com.view.jurisdictionmanage;

import com.bean.Power;
import com.bean.Role;
import com.bean.User;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;
import com.service.PowerService;
import com.service.RoleService;
import com.service.UserService;
import com.service.impl.PowerServiceImpl;
import com.service.impl.RoleServiceImpl;
import com.service.impl.UserServiceImpl;
import com.util.MyCheckBox;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RoleAddDialog extends JDialog {

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private LovoTxt nameTxt;
    private MyCheckBox roleCbx;


    private MainFrame frame ;
    private UserService userService = new UserServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    private PowerService powerService = new PowerServiceImpl();

    public RoleAddDialog(MainFrame frame) {
        // TODO Auto-generated constructor stub
        super(frame,"添加新角色",true);
        this.frame = frame;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        cbxFlush();
        this.init();
        this.setVisible(true);


    }

    public void cbxFlush(){
        String[] strs = powerService.getAllPowers();
        roleCbx = new MyCheckBox("权限",strs,20,100,this){

        };
    }

    private void init() {
        // TODO Auto-generated method stub
        nameTxt = new LovoTxt("角色",20,20,this);


        this.addBtn = new LovoButton("添加", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Role role = new Role();
                String name = nameTxt.getText();
                role.setRoleName(name);
                String[] strings = roleCbx.getItem();
                List<String> rolenames = Arrays.asList(strings);

                if(roleService.addRole(role,rolenames)){
                    JOptionPane.showMessageDialog(null,"添加成功");
                    RoleAddDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"添加失败");
                }
            }
        });

    }

}
